using Coding4Fun.Phone.Controls.Primitives;
using System;
using System.Threading;
using System.Windows.Controls;
namespace Coding4Fun.Phone.Controls.Toolkit
{
	public abstract class DataSource<T> : ILoopingSelectorDataSource where T : struct
	{
		private ValueWrapper<T> valueWrapper_0;
		private EventHandler<SelectionChangedEventArgs> eventHandler_0;
		public event EventHandler<SelectionChangedEventArgs> SelectionChanged
		{
			add
			{
				EventHandler<SelectionChangedEventArgs> eventHandler = this.eventHandler_0;
				EventHandler<SelectionChangedEventArgs> eventHandler2;
				do
				{
					eventHandler2 = eventHandler;
					EventHandler<SelectionChangedEventArgs> eventHandler3 = (EventHandler<SelectionChangedEventArgs>)Delegate.Combine(eventHandler2, value);
					eventHandler = Interlocked.CompareExchange<EventHandler<SelectionChangedEventArgs>>(ref this.eventHandler_0, eventHandler3, eventHandler2);
				}
				while (eventHandler != eventHandler2);
			}
			remove
			{
				EventHandler<SelectionChangedEventArgs> eventHandler = this.eventHandler_0;
				EventHandler<SelectionChangedEventArgs> eventHandler2;
				do
				{
					eventHandler2 = eventHandler;
					EventHandler<SelectionChangedEventArgs> eventHandler3 = (EventHandler<SelectionChangedEventArgs>)Delegate.Remove(eventHandler2, value);
					eventHandler = Interlocked.CompareExchange<EventHandler<SelectionChangedEventArgs>>(ref this.eventHandler_0, eventHandler3, eventHandler2);
				}
				while (eventHandler != eventHandler2);
			}
		}
		public virtual bool IsEmpty
		{
			get
			{
				return false;
			}
		}
		public object SelectedItem
		{
			get
			{
				return this.valueWrapper_0;
			}
			set
			{
				if (value != this.valueWrapper_0)
				{
					ValueWrapper<T> valueWrapper = value as ValueWrapper<T>;
					if (valueWrapper != null && this.valueWrapper_0 != null)
					{
						T value2 = valueWrapper.Value;
						if (value2.Equals(this.valueWrapper_0.Value))
						{
							return;
						}
					}
					object obj = this.valueWrapper_0;
					this.valueWrapper_0 = valueWrapper;
					EventHandler<SelectionChangedEventArgs> eventHandler = this.eventHandler_0;
					if (eventHandler != null)
					{
						eventHandler.Invoke(this, new SelectionChangedEventArgs(new object[]
						{
							obj
						}, new object[]
						{
							this.valueWrapper_0
						}));
					}
				}
			}
		}
		public object GetNext(object relativeTo)
		{
			T? relativeTo2 = this.GetRelativeTo(((ValueWrapper<T>)relativeTo).Value, 1);
			if (!relativeTo2.get_HasValue())
			{
				return null;
			}
			return this.valueWrapper_0.CreateNew(new T?(relativeTo2.get_Value()));
		}
		public object GetPrevious(object relativeTo)
		{
			T? relativeTo2 = this.GetRelativeTo(((ValueWrapper<T>)relativeTo).Value, -1);
			if (!relativeTo2.get_HasValue())
			{
				return null;
			}
			return this.valueWrapper_0.CreateNew(new T?(relativeTo2.get_Value()));
		}
		protected abstract T? GetRelativeTo(T relativeDate, int delta);
	}
}
